$(function(){
	
	var serverele = document.getElementById('server');
	var portele = document.getElementById('port');
	var countele = document.getElementById('count');
	var wsUrl = "ws://" + serverele.value + ":" + portele.value;
	
	initWs(wsUrl);
	
	OptionRoomBind();
	//加载下拉多选组件
	$('#usertype').selectpicker({
        'selectedText': 'cat'
    });
});


function connectionBtnClicked(){
	var serverele = document.getElementById('server');
	var portele = document.getElementById('port');
	var countele = document.getElementById('count');
	var wsUrl = "ws://" + serverele.value + ":" + portele.value;
	for (var i = 0; i < countele.value; i++){
		initWs(wsUrl);
	}
}
var group = 0;
var user = document.getElementById("userid");
var nickname = document.getElementById("nickname");
function addTGroup(dom){
	group = $(dom).data('id');
	var groupName = $(dom).data('name');
	messagsToHtml.torespBody();
	sendPacket(websockets, Command.values.COMMAND_HEARTBEAT_REQ, GroupUsersReqBody, {groupid : group+""});
	JoinGroup();
	/**head的名字**/
	$('#head-title').text(groupName);
	$('#room-back').attr("onclick", "showGroups()");
	/**隐藏添加按钮**/
	$('#add-room').addClass('hidden');
	/**隐藏组列表**/
	$('#chatGroups').addClass('hidden');
	/**显示聊天的的组件**/
	$('#chatPage').removeClass('hidden');
	/**显示在线用户组件**/
	$('#show-users').removeClass('hidden');
	$('#bs-example-navbar-collapse-1').removeClass('hidden');
}

function sendBtnClicked(){
	var textele = $('#text').val();
	if(!textele) {
		return ; 
	}
	$('#text').val('')
	var command = Command.values.COMMAND_CHAT_REQ;
	var bodyData = {
		text : textele
		,group : group+""
		,type : ChatType.values.CHAT_TYPE_PUBLIC
	};
	var ws = websockets;
	sendPacket(ws, command, ChatReqBody, bodyData);
}
var userValue = {
	users : 1
};
function loadGroupUsers(){
	
}
function showGroups(){
	/**head的名字**/
	$('#head-title').text("在线会议");
	/**显示添加按钮**/
	$('#add-room').removeClass('hidden');
	/**显示组列表**/
	$('#chatGroups').removeClass('hidden');
	/**隐藏聊天的的组件**/
	$('#chatPage').addClass('hidden');
	/**隐藏在线用户组件**/
	$('#show-users').addClass('hidden');
	$('#bs-example-navbar-collapse-1').addClass('hidden');
}

function OptionRoomBind(){
	$('.add-room-form').on('click', function(){
		var data = $('#room-form').serialize();
		var url = $('#room-form').attr('action');
		$.ajax({ url: url, data: data, success: function(res){
	        if(res.isOk){
	        	window.location.reload();
	        }
	      }});
	});
	$('.del-room').on('click', function(){
		var _this = $(this);
		$.ajax({ url: 'room/endRoom', data: {rid: _this.data("id")}, success: function(res){
	        if(res.isOk){
	        	_this.parent().remove();
	        	if(group && group != 0){
	        		JoinGroup();
	        	}
	        }
	      }});
		
	});
}

function JoinGroup(){
	var command = Command.values.COMMAND_JOIN_GROUP_REQ;
	var bodyData = {
		group : group+""
	};
	sendPacket(websockets, command, JoinReqBody, bodyData);
}

function loadTips(){
	var html = '<div class="alert alert-danger  alert-dismissible fade in" role="alert" id="room-close-tips">'
	      +'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
	      +'<strong>会议结束!</strong> 当前会议已被创建者结束'
	      +'</div>';
	$("#close-tips").html(html);
	
	$('#room-close-tips').on('closed.bs.alert', function () {
		  window.location.reload();
	})
	
}